{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python385jvsc74a57bd04c36a90a135e4aa4256adc3692917ccc380701b533777f3da43dc8bc5264a29c",
   "display_name": "Python 3.8.5 64-bit ('base': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# 第一次作业第1题\n",
    "作者：赵飞\n",
    "学号：202018019427078\n",
    "日期：2021-04-18"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "def triangle_wave(x, c, c0, hc):\n",
    "    '''周期为1的三角波计算函数'''\n",
    "    x = x - int(x)\n",
    "    if x >= c:\n",
    "        r = 0.0\n",
    "    elif x < c0:\n",
    "        r = x / c0 * hc\n",
    "    else:\n",
    "        r = (c-x) / (c-c0) * hc\n",
    "    return r\n",
    "x = np.linspace(0, 2, 1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# method-1: 使用列表表达式生成三角波\n",
    "# 先测速\n",
    "%timeit y1 = np.array([triangle_wave(t, 0.6, 0.4, 1.0) for t in x])\n",
    "y1 = np.array([triangle_wave(t, 0.6, 0.4, 1.0) for t in x])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# method-2: 使用frompyfunc封装的ufunc函数生成三角波\n",
    "triangle_ufunc1 = np.frompyfunc(triangle_wave, 4, 1)\n",
    "# 先测速\n",
    "%timeit y2 = triangle_ufunc1(x, 0.6, 0.4, 1.0)\n",
    "y2 = triangle_ufunc1(x, 0.6, 0.4, 1.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# method-3: 利用lambda函数对方式2进一步封装\n",
    "triangle_ufunc2 = np.frompyfunc(lambda x: triangle_wave(x, 0.6, 0.4, 1.0), 1, 1)\n",
    "# 先测速\n",
    "%timeit y3 = triangle_ufunc2(x)\n",
    "y3 = triangle_ufunc2(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# method-4: 使用vectorize封装的函数生成三角波\n",
    "triangle_ufunc3 = np.vectorize(triangle_wave, otypes=[np.float])\n",
    "# 先测速\n",
    "%timeit y4 = triangle_ufunc3(x, 0.6, 0.4, 1.0)\n",
    "y4 = triangle_ufunc3(x, 0.6, 0.4, 1.0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 类型转换\n",
    "y2 = y2.astype(np.float)\n",
    "y3 = y3.astype(np.float)\n",
    "# 比较四种方式生成的三角波\n",
    "print(\"y1==y2:\", np.all(y1==y2))\n",
    "print(\"y2==y3:\", np.all(y2==y3))\n",
    "print(\"y3==y4:\", np.all(y3==y4)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 绘制结果即生成的三角波\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "plt.plot(x, y1)"
   ]
  }
 ]
}